package com.bc.mcode.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

public class StringX {
    private static final String[] MONTHS = new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};

    public StringX() {
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }

    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }

    public static String getCurrentDate() {
        return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String getDateString(Date date) {
        return DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String getCurrentDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    public static String getUUID() {
        StringBuilder uuid = new StringBuilder(getCurrentDate("yyMMddHHmmssSSS"));
        uuid.append(Thread.currentThread().getId());
        uuid.append(RandomUtils.nextInt(10));
        return uuid.toString();
    }

    public static String pwd2base64(String password) {
        return Base64.encodeBase64String(password.getBytes());
    }

    public static String createFileName() {
        return RandomStringUtils.randomAlphabetic(4) + getCurrentDate("yyMMddHHmmss");
    }

    public static String getWebFileName(String fileName) {
        String tmpFileNm = FilenameUtils.separatorsToUnix(fileName);
        return tmpFileNm.substring(tmpFileNm.indexOf("/mcode"), tmpFileNm.length());
    }

    public static String getWebFileName(String fileName, String webRootPathStr) {
        String tmpFileNm = FilenameUtils.separatorsToUnix(fileName);
        return tmpFileNm.substring(tmpFileNm.indexOf(webRootPathStr), tmpFileNm.length());
    }

    public static String getWebDownFilePath(String fileName) {
        String tmpFileNm = FilenameUtils.separatorsToSystem(fileName);
        return tmpFileNm.substring(tmpFileNm.indexOf("/mcode") + "/mcode".length() + 1, tmpFileNm.length());
    }

    public static String getWebDownFilePath(String fileName, String webRootPathStr) {
        String tmpFileNm = FilenameUtils.separatorsToSystem(fileName);
        return tmpFileNm.substring(tmpFileNm.indexOf(webRootPathStr) + "/mcode".length() + 1, tmpFileNm.length());
    }

    public static String getWebDownFileName(String fileName) {
        String tmpFileNm = FilenameUtils.separatorsToSystem(fileName);
        return FilenameUtils.getName(fileName);
    }

    public static File createDiskFile(String rootPath, Integer type, String fileName) {
        if (!isEmpty(rootPath) && type != null) {
            File rootDir = new File(rootPath);
            if (rootDir.exists()) {
                String typePathName = "";
                if (type == 1) {
                    typePathName = "imgs";
                } else if (type == 2) {
                    typePathName = "videos";
                } else if (type == 5) {
                    typePathName = "helps";
                } else {
                    typePathName = "others";
                }

                File typeDir = new File(rootDir, typePathName);
                if (!typeDir.exists()) {
                    typeDir.mkdirs();
                }

                File timeDir = new File(typeDir, getCurrentDate("yyyyMMdd"));
                if (!timeDir.exists()) {
                    timeDir.mkdirs();
                }

                String typeName = fileName.substring(fileName.lastIndexOf(46), fileName.length());
                return new File(timeDir, createFileName() + typeName);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public static File createDiskFileByName(String rootPath, Integer type, String fileName, String name) {
        if (!isEmpty(rootPath) && type != null) {
            File rootDir = new File(rootPath);
            if (rootDir.exists()) {
                String typePathName = "";
                if (type == 1) {
                    typePathName = "imgs";
                } else if (type == 2) {
                    typePathName = "videos";
                } else if (type == 5) {
                    typePathName = "helps";
                } else if (type == 4) {
                    typePathName = "class";
                } else if (type == 3) {
                    typePathName = "extport";
                } else {
                    typePathName = "others";
                }

                File typeDir = new File(rootDir, typePathName);
                if (!typeDir.exists()) {
                    typeDir.mkdirs();
                }

                File timeDir = new File(typeDir, getCurrentDate("yyyyMMdd"));
                if (!timeDir.exists()) {
                    timeDir.mkdirs();
                }

                String typeName = fileName.substring(fileName.lastIndexOf(46), fileName.length());
                return new File(timeDir, name + typeName);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public static String getDiskFilePath(String rootPath, Integer type) {
        if (!isEmpty(rootPath) && type != null) {
            File rootDir = new File(rootPath);
            if (rootDir.exists()) {
                String typePathName = "";
                if (type == 1) {
                    typePathName = "imgs";
                } else if (type == 2) {
                    typePathName = "videos";
                } else if (type == 5) {
                    typePathName = "helps";
                } else if (type == 3) {
                    typePathName = "extport";
                } else {
                    typePathName = "others";
                }

                File typeDir = new File(rootDir, typePathName);
                if (!typeDir.exists()) {
                    typeDir.mkdirs();
                }

                File timeDir = new File(typeDir, getCurrentDate("yyyyMMdd"));
                if (!timeDir.exists()) {
                    timeDir.mkdirs();
                }

                return timeDir.getAbsolutePath();
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    public static String randomString(int count) {
        return RandomStringUtils.randomNumeric(count);
    }

    public static String month2month(int numMonth) {
        return MONTHS[numMonth - 1];
    }

    public static int getDaysBetweenTime(String startTime, String endTime) {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date d1 = simpleFormat.parse(startTime);
            Date d2 = simpleFormat.parse(endTime);
            int days = (int)((d2.getTime() - d1.getTime()) / 86400000L);
            return days + 2;
        } catch (ParseException var6) {
            var6.printStackTrace();
            return 0;
        }
    }

    public static void main(String[] args) {
        String t1 = "2017-09-14";
        String t2 = "2017-09-26";
        System.out.println(getDaysBetweenTime(t1, t2));
    }

    public static boolean downLoadWordFile(String fltName, String fltPath, Map<String, Object> mapData, String fileName, HttpServletRequest request, HttpServletResponse response) {
        return false;
    }

    public static boolean isOSLinux() {
        Properties prop = System.getProperties();
        String os = prop.getProperty("os.name");
        return os != null && os.toLowerCase().indexOf("linux") > -1;
    }
}
